package com.ftf.tp.blog.interfaces.consumer;


import com.ftf.tp.blog.application.dto.message.SupportMessage;
import com.ftf.tp.blog.application.service.command.ArticleCommandService;
import com.ftf.tp.common.constants.RocketMqConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * 文章点赞消息消费者
 *
 * @author tanghao
 * @date 2021/12/4
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = RocketMqConstant.SUPPORT_TOPIC, consumerGroup = RocketMqConstant.TP_BLOG_SUPPORT_CONSUMER_GROUP,
        selectorExpression = RocketMqConstant.ARTICLE_SUPPORT)
public class BlogSupportConsumer implements RocketMQListener<SupportMessage>, RocketMQPushConsumerLifecycleListener {

    @Resource
    private ArticleCommandService articleCommandService;


    @Override
    public void onMessage(SupportMessage supportMessage) {
        log.info("接受到点赞消息实体{}", supportMessage);
        articleCommandService.articleSupport(supportMessage);
    }

    /**
     * 修改consumer参数
     */
    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        //消费重试次数
        consumer.setMaxReconsumeTimes(2);
    }
}
